package lium.lbs.lbsim.utils

import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import lium.lbs.lbsim.R


/**
 * Created by machunyan on 2018/6/28.
 */
class ContactsMenuPopWindow(context: Activity) : PopupWindow() {

    private val conentView: View
    init {

        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

        conentView = inflater.inflate(R.layout.pop_contacts_popwindow, null)

        val h = context.windowManager.defaultDisplay.height

        val w = context.windowManager.defaultDisplay.width

        // 设置SelectPicPopupWindow的View

        this.contentView = conentView

        // 设置SelectPicPopupWindow弹出窗体的宽

        this.width = w / 2 -130

        // 设置SelectPicPopupWindow弹出窗体的高

        this.height = LinearLayout.LayoutParams.WRAP_CONTENT

        // 设置SelectPicPopupWindow弹出窗体可点击

        this.isFocusable = true

        this.isOutsideTouchable = true

        // 刷新状态

        this.update()

        // 实例化一个ColorDrawable颜色为半透明

        val dw = ColorDrawable(0)

        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作

        this.setBackgroundDrawable(dw)

        // 设置SelectPicPopupWindow弹出窗体动画效果

        this.animationStyle = R.style.AnimationPreview
    }


    /**
     *
     * 显示popupWindow
     *
     * @param anchor
     */

    fun showPopupWindow(anchor: View) {
        if (!this.isShowing) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(anchor, -(anchor.layoutParams.width), 0)
        } else {
            this.dismiss()
        }

    }

}
